package com.googlecode.wannabet.service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.googlecode.wannabet.domain.WannabetUser;

public class UserDetailsService implements IWannabetService {

	public String processRequest(HttpServletRequest request, HttpServletResponse response) {
		String email = request.getParameter("id");
		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Query query = new Query(WannabetUser.class.getSimpleName());
		query.addFilter("email", Query.FilterOperator.EQUAL, email);
		PreparedQuery preparedQuery = datastore.prepare(query);

		StringBuilder builder = new StringBuilder();
		builder.append("<wannabet>");

		for(Entity result : preparedQuery.asIterable() ) {
			builder.append("<user>");

			builder.append("<name>" + result.getProperty("name") + "</name>");
			builder.append("<email>" + result.getProperty("email") + "</email>");
			builder.append("<cash>" + result.getProperty("cash") + "</cash>");
			
			builder.append("</user>");
		}
		builder.append("</wannabet>");
		
		return builder.toString();
	}

}
